<?php
/**
 * SYSCONFIG
 * 
 * @version 0.2a
 */
 
// No direct access
defined( "__DEENZO__" ) or die( "deenzo$" );

// Loading dependencies
sbin( "sql" ) or kill( "sql$" );

// Loading SQL
sql( "sysconfig.s" ) or kill( "sysconfig.s$" );

function sysconfig( $opt, $val = "", $args = "" ) {

    /** Special behaviour when caller is a X11 script */
    $cll = debug_backtrace(); 
    
    $cll = $cll[2]["file"] . " | " . $cll[1]["file"];
    
    if ( is_string( $val ) and preg_match( "/^\-/", $val ) ) { 
        $args = $val; $val = ""; 
    }
    
    if ( preg_match( "/\/theme(s)?\//", $cll ) ) 
        
        $args .= ( $args ) ? "-p" : "p";
    
    eval( ARGL );
    
    // K always is Uppercase
    $k = strtoupper( $k );

    /* If V, SYSCONFIG prepare to save config */
    if ( $val ) {
    
        if ( is_array( $val ) or is_object( $val ) ) 
            $val = serialize( $val );
            
        $is_sysconfig = sql( "sysconfig.( opt = '$opt' )" );
        
        if ( $is_sysconfig )
            return sql( "sysconfig.( opt = '$opt' )", "val=$val" );
        else
            return sql( "sysconfig", "opt=$opt&val=$val" );
            
        env( "sysconfig.$opt", $val );
    
    /* Return SYSCONFIG value */
    } else {
    
        if ( env( "sysconfig.$opt" ) )
            $val = env( "sysconfig.$opt" );
        else
            $val = sql( "sysconfig.[val].( opt = '$opt' )" );
        
        if ( @unserialize( $val ) )
            $val = unserialize( $val );

        env( "sysconfig.$opt", $val );
     
        if ( $p )
            echo $val;
        
        return $val;
    
    }

}

function is_enabled( $sysconfig ) {
    return sql( "IS ENABLED", "opt=$sysconfig" );
}
    
        function is_disabled( $sysconfig ) { return !is_enabled( $sysconfig ); }